requirejs.config({
    baseUrl: '../js/libs',
    paths: {
    	
    },
    shim: { //引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'jquery': {
            exports: '$'
        }

    }
});
define(['jquery','popover'],function($){
		
	var SeniorSearch =  function SeniorSearch(el,option){
		this.$el = $(el);
		this.box =    '<div class="seniorBox">'+
								'<div class="seniorBox_cont">'+

								'</div>'+
								'<div class="seniorBox_item">'+
									'<button class="btn btn-green closeBtn">取消</button>'+
									'<button class="btn btn-green">搜索</button>'+
									'<button class="btn btn-green">重置</button>'+
								'</div>'+
							'</div>';
		
                   			
                   				
                                
                   			
                   			
                   		
		
		this.defaults = {
			background:"red", //下拉框背景颜色背景颜色
			//搜索框内容模板
			tpl:'<div class="col-md-3 col-sm-3 form-group">'+
			        '<label class="control-label">搜索1：</label>'+
			        '<div class="control-con">'+
			            '<input class="form-control" placeholder="搜索条件1">'+
			        '</div>'+                
			    '</div>'+
			    '<div class="col-md-3 col-sm-3 form-group">'+
			        '<label class="control-label">搜索2：</label>'+
			        '<div class="control-con">'+
			            '<input class="form-control" placeholder="搜索条件2">'+
			        '</div>'+                    
			    '</div>'+
			    '<div class="col-md-3 col-sm-3 form-group">'+
			        '<label class="control-label">搜索3：</label>'+
			        '<div class="control-con">'+
			            '<input class="form-control" placeholder="搜索条件3">'+
			        '</div>'+                
			    '</div>'+
			    '<div class="col-md-3 col-sm-3 form-group">'+
			        '<label class="control-label">搜索4：</label>'+
			        '<div class="control-con">'+
			            '<input class="form-control" placeholder="搜索条件4">'+
			        '</div>'+                    
			    '</div>',
			    doSomething:null,
		}
		
		// 合并默认参数和自定义参数
    	this.options = $.extend(this.defaults, option);

	}
	
	SeniorSearch.prototype = {
		
		init: function(){
			this.$el.after(this.box);
			this.$el.parent().css({position:"relative"});
			this.$el.parent().children(".seniorBox").children(".seniorBox_cont").html(this.options.tpl);
			this.$el.parent().children(".seniorBox").css({background:this.options.background});
			
			this.initBox();
			this.openBox();
			this.closeBox();
		},
		initBox: function(){
			var prLeft = this.$el.parent().offset().left;
			var elLeft = this.$el.offset().left;
			var pWidth = document.body.offsetWidth;//屏幕宽度
			var diffLeft = elLeft - prLeft;
			var elWidth = this.$el.outerWidth();//按钮宽度
			var bWidth = $(".seniorBox").outerWidth();//弹出框宽度
			if(diffLeft<=(bWidth/2)&&(pWidth-elLeft)>=(bWidth/2)){
				this.$el.parent().children(".seniorBox").css({left:0,right:"auto"})
			}else if(diffLeft>(bWidth/2)&&(pWidth-elLeft)<=(bWidth/2)){
				this.$el.parent().children(".seniorBox").css({left:"auto",right:0})
			}else if(diffLeft>(bWidth/2)&&(pWidth-elLeft)>=(bWidth/2)){
				this.$el.parent().children(".seniorBox").css({left:(diffLeft-(bWidth/2)+elWidth/2),right:"auto"})
			}
			
			
		},
		openBox: function(){
			var that = this;
			//点击高级，打开高级搜索列表
			this.$el.click(function(e){
				e.stopPropagation();
				that.initBox();
				that.$el.parent().children(".seniorBox").css({
					display:"block"
				})
			})
			//点击高级搜索列表时阻止冒泡
			this.$el.next(".seniorBox").click(function(e){
				e.stopPropagation();
			})

		},
		closeBox: function(){
			var that = this;
			$(document).click(function(){
				that.$el.parent().children(".seniorBox").css({
					display:"none"
				})
			})
			$(".closeBtn").click(function(){
				$(".seniorBox").css({
					display:"none"
				})
			})
		}
	}
	
	$.fn.SeniorSearch = function (options) {
        return this.each(function () {
            new SeniorSearch(this, options).init();
        });
    };

})